package com.nosnow.statistics.schedule;

import com.nosnow.statistics.service.StatisticsDailyService;
import com.nosnow.statistics.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author by bingHeng
 * @version 1.0
 * @Create 2020/6/1 20:56
 *
 * 创建统计数据的定时任务类
 */
@Component
public class ScheduledTask {

    @Autowired
    private StatisticsDailyService statisticsDailyService;

    // cron在线生成器， 即可生成cron表达式, spring-boot 中的cron值为6位，因为默认了当前年，所以用在线生成器生成的cron表达式，去掉最后一位就可以了

    // 每天凌晨一点执行这个定时任务
    @Scheduled(cron = "0 0 1 * * ?")
    public void task01() {
        // 获取上一天的日期
        String day = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        statisticsDailyService.registerCount(day);
    }
}
